<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container" style="margin-bottom:20px">

      <span style="margin-left:10px">用户手机号：</span>
      <el-input @keyup.enter.native="handleFilter" clearable style="width: 170px;" class="filter-item" placeholder="请输入手机号" v-model="listQuery.mobile">
      </el-input>

      <span style="margin-left:10px">VIP卡号：</span>
      <el-input @keyup.enter.native="handleFilter" clearable style="width: 170px;" class="filter-item" placeholder="请输入VIP卡号" v-model="listQuery.cardNumber">
      </el-input>

      <span style="margin-left:10px">VIP类型：</span>
      <el-select v-model="listQuery.type" clearable placeholder="请选择" style="width: 150px;">
        <el-option v-for="item in vipTypes" :key="item.type" :label="item.typeName" :value="item.type">
        </el-option>
      </el-select>

      <span style="margin-left:10px">是否VIP卡绑定：</span>
      <el-select v-model="listQuery.isCard" clearable placeholder="请选择" style="width: 100px;">
        <el-option v-for="item in cards" :key="item.id" :label="item.label" :value="item.id">
        </el-option>
      </el-select>

      <span style="margin-left:10px">生成日期：</span>
      <el-date-picker class="timeWidht" v-model="dateformat" type="daterange" align="right" unlink-panels range-separator="-" 
        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions">
      </el-date-picker>

      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>

    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="数据加载中，请稍后" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" min-width="50">
        <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="用户ID" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.userId}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" align="center" label="VIP类型" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.typeName}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="center" label="订单号" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.orderNo}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="center" label="账单号" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.paymentNo}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" align="center" label="总次数" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.sumCount}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" align="center" label="是否VIP卡绑定" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.isCard == 1 ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="center" label="VIP卡号" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.cardNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="center" label="创建时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.createTime | dateformat('YYYY-MM-DD HH:mm')}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" align="center" label="过期时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{scope.row.expiredDate | dateformat('YYYY-MM-DD HH:mm')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="{row}">
          <el-button size="small" type="danger" plain @click="deleteUserVip(row)">VIP解绑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNo" :page-sizes="[10,20,30,50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    

  </div>
</template>

<script>
import { getAllVipType, addUserVip, createUserVip, removeUserVipByMobile, removeUserVipById, queryUserVipByPage } from '@/api/vip'

export default {
  name: 'list',
  components: {},
  watch: {

  },
  data() {
    return{
      listQuery:{
        mobile:null,
        cardNumber:null,
        type:null,
        isCard:null,
        createTime:null,
        pageNo: 1,
        pageSize: 10
      },
      cards:[
        { id:0, label:'否' },
        { id:1, label:'是' }
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      vipTypes:[],
      list: null,
      total: null,
      listLoading: true,
      tableKey: 0,
      dateformat:''
    }
  },
  mounted() {
    this.getVipType()
    this.getList()
  },
  methods: {
    // 查询所有有效VIP类型
    getVipType(){
      getAllVipType().then(res => {
        if(res.code == 0){
          this.vipTypes = res.data
        }
      }).catch(error => {}
      )
    },
    // 按条件筛选
    handleFilter(){
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val
      this.getList()
    },
    // 分页查询VIP用户列表
    getList(){
      this.listLoading = true
      const params = this.listQuery
      const { dateformat } = this
      if (dateformat) {
        params['beginTime'] = dateformat[0]
        params['endTime'] = dateformat[1]
      } else {
        delete params.beginTime
        delete params.endTime
      }
      queryUserVipByPage(params).then(response => {
        const { code, data: { total, list = [] } } = response
        if (code == 0) {
          this.total = total
          this.list = list
          this.listLoading = false
        }
      }).catch(error => {})
    },
    // VIP解绑
    deleteUserVip(row){
      removeUserVipById(row.id).then(res => {
        if(res.code == 0){
          this.$notify({
            title: '成功',
            message: '解绑成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        }else{
          this.$message({
            title: '失败',
            message: message,
            type: 'error',
            duration: 2000
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.icon-ctx {
  width: 160px;
  height: 80px;
  overflow: hidden;
  margin: 0 auto;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.app-container {
  padding: 20px;
}
.pagination-container {
  margin-top: 30px;
}
.timeWidht{
  width: 300px;
}
</style>
